<template>



  <div class="flex items-center bg-white rounded-lg shadow p-3 mb-2">
    <label for="input" class="w-2/5 pr-2">{{props.Label}}
      <i class="fas fa-info-circle" v-if="props.Tips != ''">
      <q-tooltip self="top end" :delay="800" max-width="600px">
        {{ props.Tips }}
      </q-tooltip>
    </i> :</label>
    <input id="input" @focus="ClickWriteBtn" @blur="ClickOkBtn"  v-model="CurrKeyLang" type="text" class="bg-white w-3/5 px-2 py-2 border border-gray-300 rounded-md" placeholder="等待按键中...">
<!--    <button @click="ClickWriteBtn" v-if="!isWriting" class="w-1/5 ml-2 px-3 py-2 bg-gray-500 text-white rounded-md">录入</button>-->
<!--    <button @click="ClickOkBtn" v-if="isWriting" class="w-1/6 ml-2 px-3 py-2 bg-blue-500 text-white rounded-md">完成</button>-->
  </div>
</template>


<script setup lang="ts">
import {defineEmits, defineProps, onMounted, ref, watch} from 'vue'
import {useGlobalStore} from "../store/global";
import {ToastError, ToastInfo} from "../utils/Toast";
import {GetLangKbs} from "../constant/lang/LangFunc";
import {Get, GetBool} from "../../wailsjs/go/dbservice/DbConfigService";
import {StartListenKeyboard, StoptListenKeyboard} from "../../wailsjs/go/service/SystemService";
import {ConfigKeyFrontIsListenKeyboard} from "../constant/config_keys";
import {HotkeyIsUse} from "../../wailsjs/go/main/App";

let storeGlobal=ref(useGlobalStore())
let isWriting = ref(false)
let props=defineProps({
  Tips:{
    type:String,
    default:""
  },
  Label:{
    type:String,
    default:"快捷键"
  },
  FirstValue:{
    type:String,
    default:""
  },
  ConfigKey:{
    type:String,
    default:""
  },
  ConfigType:{
    type:String,
    default:"default"
  },
})


let emits=defineEmits(["OkClick"])
let CurrKeyCode =ref("")
let CurrKeyLang =ref("")
onMounted(()=>{
  SetFirstValue()


})

watch(()=>storeGlobal.value.CurrKeyBoardCombineKey.lang,(nval,oval)=>{
   if(isWriting.value && nval){
     GetBool(ConfigKeyFrontIsListenKeyboard).then(flag=>{
       if(flag){
         CurrKeyLang.value=nval
       }
     })
   }
})
function SetFirstValue(){
  //要么传入FirstValue 要么传入configkey
  if(props.FirstValue != ""){
    let codes =props.FirstValue?.split("+").map(s=> parseInt(s))
    CurrKeyCode.value=props.FirstValue
    CurrKeyLang.value=GetLangKbs(codes)
  }else{
      Get(props.ConfigKey).then(codeStr=>{
        console.log(codeStr,"codestr")
        let codes =codeStr?.split("+").map(s=> parseInt(s))
        CurrKeyCode.value=props.FirstValue
        CurrKeyLang.value=GetLangKbs(codes)
      })
  }
}

function ClickWriteBtn(){
  StartListenKeyboard()
  isWriting.value=true
  //CurrKeyLang.value=""
  storeGlobal.value.SetMHotKeyData("","")
  ToastInfo("开始监听按键")
}

function ClickOkBtn(){

  StoptListenKeyboard()
  isWriting.value=false
  if(storeGlobal.value.CurrKeyBoardCombineKey.code == ""){
    //快捷键没设置，则设为初始状态的值
    SetFirstValue()
    return
    //ToastError("未检测到自定义按键")
  }
  CurrKeyCode.value=storeGlobal.value.CurrKeyBoardCombineKey.code
  CurrKeyLang.value=storeGlobal.value.CurrKeyBoardCombineKey.lang
  HotkeyIsUse(CurrKeyCode.value).then(isUse=>{
    if(isUse){
      ToastError(  "【"+CurrKeyLang.value+"】  已经被占用")
      //快捷键没设置，则设为初始状态的值
      SetFirstValue()
    }else{
      // 将快捷键的code和lang通知给父组件
      emits("OkClick",
          storeGlobal.value.CurrKeyBoardCombineKey.code,
          storeGlobal.value.CurrKeyBoardCombineKey.lang,
          props.ConfigKey
      )
    }
  })
  


  //ToastInfo("快捷键【"+storeGlobal.value.CurrKeyBoardCombineKey.lang+"】设置完成")
}



</script>
<style scoped>

</style>